[20220826_163752_user] D2_main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. uint16_t M4_AD_Value = 0;
  40. float M4_voltage_V =0;
  41. float M4_pH_mid = 1.500;
  42. float M4_pH_low = 2.030;
  43. float M4_pH_high =0.975;
  44. float M4_PH=0;
  45. uint16_t M5_AD_Value = 0;
  46. float M5_voltage_V =0;
  47. float M5_DO_offset =0.44;
  48. float M5_DO =0;
  49. uint16_t M7_AD_Value = 0;
  50. float M7_voltage_V =0;
  51. float M7_ORP_offset =0;
  52. float M7_ORP =0;
  53. /* USER CODE BEGIN PV */
  54. typedef void (*pFunction)(void);
  55. /* USER CODE END PV */
  56. /* Private function prototypes -----------------------------------------------*/
  57. void SystemClock_Config(void);
  58. void MX_ADC1_Init1(char pin);
  59. void sensor(void);
  60. /* USER CODE BEGIN PFP */
  61. /* USER CODE END PFP */
  62. /* Private user code ---------------------------------------------------------*/
  63. /* USER CODE BEGIN 0 */
  64. int _write(int fd, char *ch, int len)
  65. {
  66. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  67. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  68. return len;
  69. }
  70. void print(char* s, float c)
  71. { int w;
  72. int w_d;
  73. int w_f;
  74. if(c<0)
  75. {
  76. w = c*100;
  77. w_d = w/100;
  78. w_f = (-1*w)%100;
  79. }
  80. else
  81. {
  82. w = c*100;
  83. w_d = w/100;
  84. w_f = w%100;
  85. }
  86. printf("%s = %d.%d\n",s,w_d,w_f);
  87. }
  88. /* USER CODE END 0 */
  89. /**
  90. * @brief The application entry point.
  91. * @retval int
  92. */
  93. int main(void)
  94. {
  95. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  96. HAL_Init();
  97. SystemClock_Config();
  98. MX_GPIO_Init();
  99. MX_USART2_UART_Init();
  100. MX_UART4_Init();
  101. MX_USART1_UART_Init();
  102. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  103. HAL_Delay(1000);
  104. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  105. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  106. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  107. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  108. {
  109. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  110. }
  111. else if(zigbeestatu[18]== 0x05)
  112. {
  113. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  114. }
  115. while (1)
  116. {
  117. sensor();
  118. if(M4_PH<16)
  119. {
  120. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_RESET);
  121. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  122. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  123. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  124. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  125. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  126. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  127. HAL_Delay(5000);
  128. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_SET);
  129. }
  130. if(M5_DO<16)
  131. {
  132. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  133. HAL_Delay(5000);
  134. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_SET);
  135. }
  136. if(M7_ORP<16)
  137. {
  138. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  139. HAL_Delay(5000);
  140. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_SET);
  141. }
  142. }
  143. }
  144. void SystemClock_Config(void)
  145. {
  146. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  147. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  148. __HAL_RCC_PWR_CLK_ENABLE();
  149. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  150. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  151. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  152. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  153. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  154. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  155. RCC_OscInitStruct.PLL.PLLM = 8;
  156. RCC_OscInitStruct.PLL.PLLN = 72;
  157. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  158. RCC_OscInitStruct.PLL.PLLQ = 3;
  159. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  160. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  161. {
  162. Error_Handler();
  163. }
  164. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  165. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  166. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  167. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  168. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  169. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  170. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  171. {
  172. Error_Handler();
  173. }
  174. }
  175. void user_pwm_setvalue(uint16_t value)
  176. {
  177. TIM_OC_InitTypeDef sConfigOC;
  178. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  179. sConfigOC.Pulse = value;
  180. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  181. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  182. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  183. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  184. }
  185. void user_pwm_setvalue2(uint16_t value)
  186. {
  187. TIM_OC_InitTypeDef sConfigOC;
  188. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  189. sConfigOC.Pulse = value;
  190. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  191. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  192. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  193. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  194. }
  195. void sensor(void)
  196. {
  197. MX_ADC1_Init1(ADC_CHANNEL_0);
  198. HAL_ADC_Start(&hadc1);
  199. HAL_ADC_PollForConversion(&hadc1, 50);
  200. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
  201. {
  202. M4_AD_Value = HAL_ADC_GetValue(&hadc1);
  203. M4_voltage_V = M4_AD_Value*3.3f/4096;
  204. if (M4_voltage_V > M4_pH_mid)
  205. {
  206. M4_PH = 7.0 - 3.0 / (M4_pH_low - M4_pH_mid) * (M4_voltage_V - M4_pH_mid);
  207. }
  208. else
  209. {
  210. M4_PH = 7.0 - 3.0 / (M4_pH_mid - M4_pH_high) * (M4_voltage_V - M4_pH_mid);
  211. }
  212. }
  213. print("M4_PH",M4_PH);
  214. MX_ADC1_Init1(ADC_CHANNEL_3);
  215. HAL_ADC_Start(&hadc1);
  216. HAL_ADC_PollForConversion(&hadc1, 50);
  217. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
  218. {
  219. M5_AD_Value = HAL_ADC_GetValue(&hadc1);
  220. M5_voltage_V = M5_AD_Value*3.3f/4096;
  221. M5_DO= (((M5_AD_Value*3.3f/4096)*100)/M5_DO_offset);
  222. }
  223. print("M5_DO",M5_DO);
  224. MX_ADC1_Init1(ADC_CHANNEL_4);
  225. HAL_ADC_Start(&hadc1);
  226. HAL_ADC_PollForConversion(&hadc1, 50);
  227. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
  228. {
  229. M7_AD_Value = HAL_ADC_GetValue(&hadc1);
  230. M7_voltage_V = M7_AD_Value*3.3f/4096;
  231. M7_ORP= (((M7_AD_Value*3.3f/4096)-(1.5+M7_ORP_offset)));
  232. }
  233. print("M7_ORP",M7_ORP);
  234. HAL_Delay(10000);
  235. }
  236. void MX_ADC1_Init1(char pin)
  237. {
  238. ADC_ChannelConfTypeDef sConfig = {0};
  239. hadc1.Instance = ADC1;
  240. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  241. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  242. hadc1.Init.ScanConvMode = DISABLE;
  243. hadc1.Init.ContinuousConvMode = DISABLE;
  244. hadc1.Init.DiscontinuousConvMode = DISABLE;
  245. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  246. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  247. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  248. hadc1.Init.NbrOfConversion = 1;
  249. hadc1.Init.DMAContinuousRequests = DISABLE;
  250. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  251. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  252. {
  253. Error_Handler();
  254. }
  255. sConfig.Channel = pin;
  256. sConfig.Rank = 1;
  257. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  258. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  259. {
  260. Error_Handler();
  261. }
  262. }
  263. void MX_GPIO_Input1(unsigned long pin)
  264. {
  265. GPIO_InitTypeDef GPIO_InitStruct = {0};
  266. __HAL_RCC_GPIOE_CLK_ENABLE();
  267. GPIO_InitStruct.Pin = pin;
  268. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  269. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  270. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  271. }
  272. void Error_Handler(void)
  273. {
  274. }